<?php
/**
 * Created by PhpStorm.
 * User: j28981
 * Date: 2015/8/22
 * Time: 10:05
 * 将上传的大图制作成较小 的缩略图(50*50)，可以设定缩略图保存在原图同目录下，或是新建目录
 */
class Image_thumb {
    private $image_origin_dir;   //原图保存绝对路径
    private $image_origin_path;   //原图保存绝对路径+文件名
    private $image_origin_ext;  //原图片的后缀名
    private $image_allow_exts;  //图片允许后缀名，可在配置文件中设定
    private $image_thumb_pre;   //区别原图，给缩图加上一前缀,可在配置文件中设定
    private $image_thumb_folder;   //一个专门存放缩图的文件夹,可在配置文件中设定
    private $image_thumb_dir;   //缩略图保存绝对路径，与image_thumb_floder有关
    private $image_thumb_path;   //缩略图保存绝对位置
    private $image_thumb_url='';  // 缩略图相对路径+文件名（upload/xxx/thumb/xx.jpg），用于保存数据库用
    private $image_thumb_canvas_width;  //规定画布的宽度，可在配置文件中设定
    private $image_thumb_canvas_height;
    private $image_canvas_whp;   //画布宽高比
    private $image_origin_width;
    private $image_origin_height;
    private $image_origin_whp;
    public $error;

    /**
     * @param String $image_origin_url : 原图的相对路径（文件上传返回的并保存的数据库中的）
     */
    public function __construct($image_origin_url){
        $this->image_origin_path = SITE_ROOT.DS.$image_origin_url;
        $image_origin_name = ltrim(strrchr($image_origin_url,'/'),'/');
        $this->image_origin_dir = dirname($this->image_origin_path);
        $this->image_thumb_canvas_width = $GLOBALS['config']['image_thumb']['image_thumb_canvas_width'];
        $this->image_thumb_canvas_height = $GLOBALS['config']['image_thumb']['image_thumb_canvas_height'];
        $this->image_thumb_folder = $GLOBALS['config']['image_thumb']['image_thumb_folder'];
        $this->image_thumb_pre = $GLOBALS['config']['image_thumb']['image_thumb_pre'];
        $this->image_allow_exts = $GLOBALS['config']['image_thumb']['image_allow_exts'];
        $this->image_thumb_dir =  $this->makeThumbDir($this->image_thumb_folder);
        $this->image_thumb_path = $this->image_thumb_dir.DS.$this->image_thumb_pre.$image_origin_name;
        $this->image_thumb_url = strrchr($this->image_thumb_path,'Upload');
        $this->image_canvas_whp =$this->image_thumb_canvas_width /$this->image_thumb_canvas_height;
        $image_origin_info = getimagesize($this->image_origin_path);
        $this->image_origin_width = $image_origin_info[0];
        $this->image_origin_height = $image_origin_info[1];
        $this->image_origin_whp =$this->image_origin_width/$this->image_origin_height;
        $this->image_origin_ext = ltrim(strrchr($image_origin_url,'.'),'.');
        if($this->image_origin_ext=='jpg'){
            $this->image_origin_ext='jpeg';
        }
    }
    private function makeThumbDir($image_thumb_folder){
        if(empty($image_thumb_folder)){
            $image_thumb_dir=$this->image_origin_dir;
        }else{
            $image_thumb_dir = $this->image_origin_dir.DS.$image_thumb_folder;
            if(!is_dir($image_thumb_dir)){
                mkdir($image_thumb_dir);
            }
        }
        return $image_thumb_dir;
    }
    /**
     *
     * @return mixed : 制作正常，则返回$this->image_thumb_url，有错误返回false
     */
    public function makeThumb(){
        if(strpos($this->image_allow_exts,$this->image_origin_ext)===false){
            $this->error = '原图片类型不正确';
            return false;
        }
        // 字符串类型，保存函数名
        $img_src_func = "imagecreatefrom{$this->image_origin_ext}";
        //图片资源
        $img_src_resource = $img_src_func($this->image_origin_path);
        //画布资源
        $img_dst_resource = imagecreatetruecolor($this->image_thumb_canvas_width,$this->image_thumb_canvas_height);
        $canvars_bg_color = imagecolorallocate($img_dst_resource,255,255,255);
        imagefill($img_dst_resource,0,0,$canvars_bg_color);
        /*图片放入画布中时，默认是铺满的，这样图片会发生变形，所以要根据两者宽高比关系，来确定图片高宽等比例缩小的宽高及在画面中的位置 S*/
        if($this->image_origin_whp >= $this->image_canvas_whp){
            //确定为横式图
            $image_thumb_width = $this->image_thumb_canvas_width;
            $image_thumb_height = round($image_thumb_width/$this->image_origin_whp);
        }else{
            $image_thumb_height = $this->image_thumb_canvas_height;
            $image_thumb_width = round($image_thumb_height*$this->image_origin_whp);
        }
        // 缩略图在画布中，相对画布的位置
        $canvas_pos_x = ceil(($this->image_thumb_canvas_width - $image_thumb_width)/2);
        $canvas_pos_y = ceil(($this->image_thumb_canvas_height - $image_thumb_height)/2);
        /* E*/
        imagecopyresampled($img_dst_resource,$img_src_resource,$canvas_pos_x,$canvas_pos_y,0,0,$image_thumb_width,$image_thumb_height,$this->image_origin_width,$this->image_origin_height);
        $image_save_func = "image{$this->image_origin_ext}";
        if($image_save_func($img_dst_resource,$this->image_thumb_path)){
            imagedestroy($img_src_resource);
            imagedestroy($img_dst_resource);
            return $this->image_thumb_url;
        }else{
            imagedestroy($img_src_resource);
            imagedestroy($img_dst_resource);
            $this->error = "缩略图制作失败，系统原因";
            return false;
        }

    }

}